<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card 卡片视图区域 -->
    <el-card>
      <!-- 搜索框区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- tabs 表格区域 -->
      <el-table :data="OrderList" style="width: 100%" stripe border>
        <el-table-column align="center" label="#" type="index"> </el-table-column>
        <el-table-column align="center" prop="order_number" label="订单编号"> </el-table-column>
        <el-table-column align="center" prop="order_price" label="订单价格" width="95px"> </el-table-column>
        <el-table-column align="center" prop="pay_status" label="是否支付" width="95px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger" size="mini">未付款</el-tag>
            <el-tag v-else type="success" size="mini">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="is_send" label="是否发货" width="95px"> </el-table-column>
        <el-table-column align="center" prop="update_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.update_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="180px">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改地址" placement="top" :enterable="false">
              <!-- 修改按钮 -->
              <el-button type="primary" icon="el-icon-edit" circle @click="editOrder(scope.row.order_id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="物流状态" placement="top" :enterable="false">
              <!-- 物流按钮 -->
              <el-button type="success" icon="el-icon-location" circle @click="getWuliliu"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页区域视图 -->
      <!-- handleSizeChange 函数在修改当前页面显示多少数据的时候触发 -->
      <!-- handleCurrentChange 函数在修改当前页码的时候触发 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[7, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改的dialog对话框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressVisible" width="50%" @close="dialogVisibleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="order_number">
          <el-cascader class="cityLotain" v-model="parentKeys" :options="options" :props="{ expandTrigger: 'hover' }"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 点击物流信息弹出的对话框 -->
    <el-dialog title="物流详情" :visible.sync="WiluydialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in wuliuInfo" :key="index" :timestamp="activity.ftime">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import options from './option'
export default {
  data() {
    return {
      queryInfo: {
        // 搜索关键字
        query: '',
        // 当前页码
        pagenum: 1,
        // 每一页显示多少条
        pagesize: 10
      },
      // 存储订单的数据的数组
      OrderList: [],
      // 存储一个多少个订单
      total: 0,
      // 控制修改地址对话框的显示与隐藏
      editAddressVisible: false,
      // 地址库
      options,
      // 存储级联选择器选中的地址数组
      parentKeys: [],
      // 储存当前将要修改的数据
      editForm: {
        address: ''
      },
      // 修改弹出框的规则效验
      editFormRules: {
        order_number: [{ required: true, message: '请选择地区', trigger: 'blur' }],
        address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      // 物流详情的显示与隐藏
      WiluydialogVisible: false,
      // 存储物流信息
      wuliuInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('请求订单列表失败')
      console.log(res.data)
      this.OrderList = res.data.goods
      this.total = res.data.total
    },
    // 点击显示多好条数据是触发
    handleSizeChange(newPagesize) {
      this.queryInfo.pagesize = newPagesize
      this.getOrderList()
    },
    // 修改页码的时候触发
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getOrderList()
    },
    // 点击修改按钮显示出来dialog弹出框，并查询得到信息
    async editOrder(orderId) {
      this.editAddressVisible = true
    },
    // 物流信息的获取与展示
    async getWuliliu() {
      const { data: res } = await this.$http.get('/kuaidi/' + 1106975712662)
      if (res.meta.status !== 200) return this.$message.error('获取物流信息失败')
      console.log(res.data)
      this.wuliuInfo = res.data
      this.WiluydialogVisible = true
    },
    // 监听修改地址对话框的关闭
    dialogVisibleClose() {
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.cityLotain {
  width: 100%;
}
</style>
